package linkedlistdemo;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-11-11
 * Time：23:04
 */
public class MyLinkedList {
    private ListNode head;
    private ListNode tail;
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            tail = node;
        } else {
            node.next = head;
            node.prev = null;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            tail = node;
        }else {
            node.prev = tail;
            node.next = null;
            tail.next = node;
            tail = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    private boolean invalidIndex(int index) {
        return (index < 0 || index >= size());
    }
    private ListNode searchIndex(int index) {
        if (invalidIndex(index)) {
            throw new IndexOutOfException("索引值越界异常！");
        }
        ListNode ret = head;
        while (index > 0) {
            ret = ret.next;
            index--;
        }
        return ret;
    }
    public void addIndex(int index,int data){
        if (index == 0) {
            this.addFirst(data);
            return;
        }
        if(index == size()) {
            this.addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public ListNode contains(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                break;
            }
            cur = cur.next;
        }
        return cur;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = contains(key);
        if (cur == null) {
            return;
        }
        //只有一个节点
        if (cur.prev == null && cur.next == null) {
            head = null;
            tail = null;
        } else {
            //有一个以上节点
            if (cur.prev == null) {
                //没有前驱节点
                head = cur.next;
                cur.next.prev = null;
            } else if (cur.next == null) {
                //没有后驱节点
                tail = cur.prev;
                cur.prev.next = null;
            } else {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
        }

     }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null || head.next == null) {
            remove(key);
        }
        ListNode cur = head.next;
        //全是中间节点的情况
        while (cur.next!=null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
        //单独处理头节点
        if (head.val == key) {
            head.next.prev = null;
            head = head.next;
        }
        if (head.prev == null && head.next == null) {
            //删除完头节点后只剩一个节点的情况
            if (tail.val == key) {
                head = null;
                tail = null;
            }
            return;
        }

        //单独处理尾节点
        if (tail.val == key) {
            tail.prev.next = null;
            tail = tail.prev;
        }
    }
    //得到单链表的长度
    public int size(){
        int ret = 0;
        ListNode cur = head;
        while (cur!=null) {
            ret++;
            cur = cur.next;
        }
        return ret;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = next;
        }
        head = tail = null;
    }
}
